﻿
#include<stdio.h>
#include<math.h>
double f(double x)
{
    return x;
}

double g(double x)
{
    return x*x;
}
/**函数指针实现的通用一元函数的定积分计算*/
double integrate(double(*f)(double),double s,double e)
{
    double dx=(e-s)/100,x,sum=0;
    for(x=s; x<e; x+=dx)
        s+=(f(x)+f(x+dx))/2*dx;// 梯形方法
    return s;
}

int main()
{
    printf("%.5f\n",integrate(f,0,1));
    printf("%.5f\n",integrate(g,0,1));
    printf("%.5f\n",integrate(sin,0,1));
    printf("%.5f\n",integrate(cos,0,1));
    printf("%.5f\n",integrate(exp,0,1));
    return 0;
}
